/**
 * @author 铭
 * @TODO 11/1/2024
 */
package com.woniuxy.services.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.daos.TbShareholderDao;
import com.woniuxy.daos.TbUserDao;
import com.woniuxy.daos.TbUserRoleDao;
import com.woniuxy.entitys.*;
import com.woniuxy.services.ShareholderService;
import org.apache.ibatis.annotations.Options;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class ShareholderServiceImpl implements ShareholderService {
    @Resource
    private TbShareholderDao tbShareholderDao;
    @Resource
    private TbUserDao tbUserDao;
    @Resource
    private TbUserRoleDao tbUserRoleDao;
    @Override
    public PageInfo<TbShareholder> selAll(String name, Integer pageNum, List<String> dateArr) {
        PageHelper.startPage(pageNum,10);
        TbShareholderExample tbShareholderExample = new TbShareholderExample();
        TbShareholderExample.Criteria criteria = tbShareholderExample.createCriteria();
        if (!StringUtils.isEmpty(name)){
            criteria.andRealnameLike("%" + name + "%");

        }
        criteria.andSoftdelEqualTo(1);
        if (dateArr!=null&&!dateArr.isEmpty()){
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            ArrayList<Date> objects = new ArrayList<>();
            for (String s : dateArr) {
                try {
                    objects.add(simpleDateFormat.parse(s));
                } catch (ParseException e) {
                    throw new RuntimeException(e);
                }
            }
            criteria.andJoinTimeBetween(objects.get(0),objects.get(1));
        }
        List<TbShareholder> tbShareholders = tbShareholderDao.selectByExample(tbShareholderExample);

        return new PageInfo<>(tbShareholders);
    }

    @Override
    public int refDel(Integer id) {
        TbShareholder tbShareholder = new TbShareholder();
        tbShareholder.setSid(id);
        tbShareholder.setSoftdel(0);
        return tbShareholderDao.updateByPrimaryKeySelective(tbShareholder);
    }

    @Override
    @Options(useGeneratedKeys=true, keyColumn="id")
    public int addShare(TbUser user) {
        user.setUpwd("123456");
        int i = tbUserDao.insertSelective(user);
        TbShareholder tbShareholder = new TbShareholder();
        tbShareholder.setSoftdel(1);
        tbShareholder.setUid(user.getUid());
        tbShareholder.setJoinTime(new Date());
        int i1 = tbShareholderDao.insertSelective(tbShareholder);

        TbUserRole tbUserRole = new TbUserRole();
        tbUserRole.setRid(3);
        tbUserRole.setUid(user.getUid());
        int i2 = tbUserRoleDao.insertSelective(tbUserRole);

        return i + i1 + i2;
    }

    @Override
    public int edit(TbShareholder shareholder) {
        return tbShareholderDao.updateByPrimaryKeySelective(shareholder);
    }

    @Override
    public int getallshareholder() {
        return tbShareholderDao.getallshareholder();
    }

    @Override
    public List<Map> getprofit() {
        List<TbShareholder> shareholders=tbShareholderDao.getprofit();
        List<Map>  getprofit = new ArrayList<>();
        for(TbShareholder tbShareholder:shareholders){
            Map<String,Object> profit=new HashMap<>();
            profit.put("name",tbShareholder.getRealname());
            profit.put("value",tbShareholder.getProfit());
            getprofit.add(profit);
        }

        return getprofit;
    }

    @Override
    public TbShareholder selShareholderByUid(Integer uid) {
        TbShareholder shareholder = tbShareholderDao.selTbShareholderByUid(uid);
        return shareholder;
    }

    @Override
    public int updataShareholder(TbShareholder shareholder) {
        return tbShareholderDao.updateShareholder(shareholder);
    }
}
